package ink.lovejinhu.GoldenCodeOfProgrammerInterview;

/**
 * @author jinhu
 * created 2021-08-10 8:56
 */
public class Interview_0105 {
    public boolean oneEditAway(String first, String second) {
        if (first.equals(second)) {
            return true;
        }
        if (Math.abs(first.length() - second.length()) >= 2) {
            return false;
        } else if (first.length() - second.length() == 1) {  //删除
            int count = 0;
            for (int i = 0, j = 0; i < second.length(); ) {
                if (first.charAt(j) != second.charAt(i)) {  //不等
                    j = j + 1;
                    count++;
                    if (count > 1) {
                        return false;
                    }
                } else {
                    j++;
                    i++;
                }
            }
            return true;
        } else if (first.length() - second.length() == -1) {
            int count = 0;
            for (int i = 0, j = 0; i < first.length(); ) {
                if (first.charAt(i) != second.charAt(j)) {  //不等
                    j = j + 1;
                    count++;
                    if (count > 1) {
                        return false;
                    }
                } else {
                    j++;
                    i++;
                }
            }
            return true;
        } else {
            int replace = 0;
            for (int i = 0; i < first.length(); i++) {
                if (first.charAt(i) != second.charAt(i)) {
                    replace++;
                    if (replace > 1) {
                        return false;
                    }
                }
            }
            return true;
        }
    }
}
